<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport"
	content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="renderer" content="webkit">
<link rel="stylesheet" href="<%=path%>/css/pintuer.css">
<link rel="stylesheet" href="<%=path%>/css/admin.css">
<link rel="shortcut icon" href="<%=path%>/images/login.jfif">
<script src="<%=path%>/js/jquery.js"></script>
<!-- 引入zTree插件 -->
<link rel="stylesheet" type="text/css"
	href="<%=path%>/third/zTree_v3/css/zTreeStyle/zTreeStyle.css">
<script type="text/javascript"
	src="<%=path%>/third/zTree_v3/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript"
	src="<%=path%>/third/zTree_v3/js/jquery.ztree.core.js"></script>
<script type="text/javascript"
	src="<%=path%>/third/zTree_v3/js/jquery.ztree.excheck.js"></script>
<script type="text/javascript">
</script>

</head>

<body>
  <div class="panel admin-panel">
  <div class="panel-head" id="add"><strong><span class="icon-pencil-square-o"></span>增加农合机构信息</strong></div>
  <div class="body-content">
    <form id="form-addrole" class="form-x">  
      <div class="form-group">
        <div class="label">
          <label>农合机构编号：</label>
        </div>
        <div class="field">
          <input type="text" class="input w50" value="" name="rural_cms_office_id"  id="rural_cms_office_id_input" data-validate=" required:请输入行政区域编号" />
          <span></span>
          <div class="tips"></div>
        </div>
      </div>
       <div class="form-group">
        <div class="label">
          <label>农合机构名称：</label>
        </div>
        <div class="field">
          <input type="text" class="input w50" value="" name="rural_cms_office_name"  id="rural_cms_office_name_input" data-validate=" required:请输入行政区域编号" />
          <span></span>
          <div class="tips"></div>
        </div>
      </div>
      <div class="form-group">
        <div class="label">
          <label>农合机构级别：</label>
        </div>
        <div class="field">
          <input type="text" class="input w50" value="" name="rural_cms_office_rank"  id="rural_cms_office_rank_input" data-validate=" required:请输入行政区域编号" />
          <span></span>
          <div class="tips"></div>
        </div>
      </div>
      
      <div class="form-group">
        <div class="label">
          <label>所属行政区域：</label>
        </div>
        <div class="field">
          <input type="hidden" id="area_id" name="area_id" value="" /> 
          <input type="hidden" id="area_rank" name="area_rank" value="" />
		    <div>
	           <ul id="zTree" class="ztree"></ul>
	        </div>
          <div class="tips"></div>
        </div>
      </div>
    
      <div class="form-group">
        <div class="label">
          <label></label>
        </div>
        <div class="field">
          <button id="add_btn" class="button bg-main icon-check-square-o" type="button"  onclick="addRuralCmsOffice()"> 提交</button>
        </div>
      </div>
    </form>
  </div>
</div>


<script type="text/javascript">
            //异步加载树形行政区域信息
 var setting = {
   data: {
                                simpleData: {
                               enable: true,  
                               idKey: "area_id",  
                               pIdKey: "father_id",   
                               rootPId: -1  
                                },
                                key: {
 	                              name: "area_name" 
	                          }
                             },
		check : {
 			enable : true,
			chkStyle: "checkbox",
 		    chkboxType: { "Y": "", "N": "s" }
 		},

 		async : {
 			type : "post",
			enable : true,
			url : "<%=path%>/area/areadata_load",  
 		}
	};
 	$(document).ready(function() {
		$.fn.zTree.init($("#zTree"), setting);
	});
 //校验机构名称是否重复
	$("#rural_cms_office_name_input").change(function() {
		var office_name = this.value;
		$.ajax({
		          url:"<%=path%>/ruralcmsoffice/ruralcmsoffice_check",
		          type:"post",
		          data:{office_name: office_name},
		          success:function(res){
		          console.log(res);
		          if(res.code==100){
		          show_validate_msg("#rural_cms_office_name_input", "success", "机构名称可用");
		          }else{
		          show_validate_msg("#rural_cms_office_name_input", "error", "机构名称不可用");
		          }
		          
		          }
		
		})
	});
//校验结果提示信息
	function show_validate_msg(ele, status, msg) {
		//清除当前元素的校验状态
		$(ele).next("span").text("");
		if ("success" == status) {
			$(ele).next("span").text(msg);
		} else if ("error" == status) {
			$(ele).next("span").text(msg);
		}
	}    
      //添加机构信息
    function addRuralCmsOffice(){
    var office_id = $("#rural_cms_office_id_input").val();
    var office_name = $("#rural_cms_office_name_input").val();
    var office_rank = $("#rural_cms_office_rank_input").val();
    var treeObj = $.fn.zTree.getZTreeObj("zTree");
    var nodes = treeObj.getCheckedNodes(true);
    
    if(nodes.length == 0 ){
		alert("请选择机构所属行政区域!")
		}else{
		var area_id = "";
		var area_rank = 0;
		$.each(nodes,function(i,node){
		   area_id+=node.area_id;
		   area_rank+=node.area_rank;
		});
		
		$.ajax({
		     type:"post",
             url:"<%=path%>/ruralcmsoffice/ruralcmsoffice_add_action",		 
		    data:{office_id: office_id,
		              office_name:office_name,
		              office_rank:office_rank,
		              area_id:area_id,
		              area_rank: area_rank,
		              },
 		    type:"post",
		    traditional:true,
		    success:function(res){
 		    if(res.code==100){
 		              alert("添加成功");
		              }else{
		               alert("乡镇农合经办点只属于乡镇一级，农合经办中心只属于县一级");
		              }
 		    }
		});
		}
		
    }
</script>

</body>
</html>
